package com.rf.richfitwheel.workflow.service.impl;

import com.rf.richfitwheel.admin.sys.model.OrgHasChildren;
import com.rf.richfitwheel.admin.sys.model.OrgHasParent;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.common.contants.Constant;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.UuidUtil;
import com.rf.richfitwheel.workflow.contants.StationProcessContant;
import com.rf.richfitwheel.workflow.dao.WfAuthRuleMapper;
import com.rf.richfitwheel.workflow.dao.WfStationProcessMapper;
import com.rf.richfitwheel.workflow.dao.WfStationProcessOrderMapper;
import com.rf.richfitwheel.workflow.dao.WfStationProcessParPubMapper;
import com.rf.richfitwheel.workflow.feignclient.SysOrgFeignService;
import com.rf.richfitwheel.workflow.model.*;
import com.rf.richfitwheel.workflow.service.ModelService;
import com.rf.richfitwheel.workflow.service.WfProcessFolderService;
import com.rf.richfitwheel.workflow.service.WfStationProcessOrderService;
import org.activiti.engine.repository.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 设置下级岗位流程的线程，使用递归调用直至末级或设置了不参与下级的机构。只处理原来已发布流程的记录，原来没有发布流程的不会自动发布
 * @author hushouquan
 */
public class ReloadChildrenStationProcess implements Runnable{
    
    protected static final Logger log = LoggerFactory.getLogger(ReloadChildrenStationProcess.class);
    
    private WfStationProcessMapper wfStationProcessMapper;
    private WfProcessFolderService wfProcessFolderService;
    private WfStationProcessOrderService wfStationProcessOrderService;
    private ModelService modelService;
    private SysOrgFeignService sysOrgFeignService;
    private WfAuthRuleMapper wfAuthRuleMapper;
    private WfStationProcessOrderMapper wfStationProcessOrderMapper;
    private String orgId;
    private String orgCode;
    private String folderCode;
    private String folderName;
    private UserVO userVO;
    private WfStationProcessParPubMapper wfStationProcessParPubMapper;
    /**
     * 全参构造用于初始化线程
     * @param wfStationProcessMapper wfStationProcessMapper
     * @param wfProcessFolderService 流程目录api
     * @param wfStationProcessOrderService 流程岗位api
     * @param modelService 模型方法
     * @param sysOrgFeignService 获取机构信息的接口
     * @param wfAuthRuleMapper 授权规则dao
     * @param wfStationProcessOrderMapper 流程岗位dao
     * @param orgId 机构id
     * @param orgCode 机构编码
     * @param folderCode 流程目录编号
     * @param folderName 流程名称
     * @param userVO 当前操作人
     * @param wfStationProcessParPubMapper WfStationProcessParPubMapper
     */
    public ReloadChildrenStationProcess(WfStationProcessMapper wfStationProcessMapper, WfProcessFolderService wfProcessFolderService, WfStationProcessOrderService wfStationProcessOrderService, ModelService modelService, SysOrgFeignService sysOrgFeignService, WfAuthRuleMapper wfAuthRuleMapper, WfStationProcessOrderMapper wfStationProcessOrderMapper, String orgId, String orgCode, String folderCode, String folderName, UserVO userVO, WfStationProcessParPubMapper wfStationProcessParPubMapper){
        this.wfStationProcessMapper = wfStationProcessMapper;
        this.wfProcessFolderService = wfProcessFolderService;
        this.wfStationProcessOrderService = wfStationProcessOrderService;
        this.modelService = modelService;
        this.sysOrgFeignService = sysOrgFeignService;
        this.wfAuthRuleMapper = wfAuthRuleMapper;
        this.wfStationProcessOrderMapper = wfStationProcessOrderMapper;
        this.orgId = orgId;
        this.orgCode = orgCode;
        this.folderCode = folderCode;
        this.folderName = folderName;
        this.userVO = userVO;
        this.wfStationProcessParPubMapper = wfStationProcessParPubMapper;
    }
    
    /**
     * 重设下级的岗位流程
     *
     * @see Thread#run()
     */
    @Override
    public void run(){
        try{
            List<OrgHasChildren> treeList = sysOrgFeignService.treeListIncludeDeptByIdDsb(this.orgId);
            log.info("开始处理下级流程审批岗位................");
            long start = System.currentTimeMillis();
            reloadChildrenStationProcess(treeList.get(0).getChildren(), this.orgCode);
            long end = System.currentTimeMillis();
            log.info("处理下级流程审批岗位完成，共用时" + (end-start)/1000 + "秒................");
        }catch(Exception e){
            log.error(e.getMessage(),e);
        }
    }
    
    /**
     * 递归处理下级已发布的流程
     * @param children 机构编码
     */
    protected void reloadChildrenStationProcess(List<OrgHasChildren> children, String orgCode) throws Exception{
        WfStationProcess process = getWfStationProcess(orgCode);
        if(process != null){
            //上级设置的岗位
            List<WfStationProcessOrder> orderList = wfStationProcessOrderService.selectListByStationProcessId(process.getId());
            for(OrgHasChildren org : children){
                String tempParSub = process.getParSub();
                String tempRuleCondition = process.getRuleCondition();
                //先看看上级有没有为自己特殊指定参与条件，有多条只取其中一条,然后用这个条件去替换主设置
                WfStationProcessParPubExample exampleParent = new WfStationProcessParPubExample();
                WfStationProcessParPubExample.Criteria criteriaParent = exampleParent.createCriteria();
                criteriaParent.andStationProcessIdEqualTo(process.getId());
                criteriaParent.andOrgIdEqualTo(org.getId());
                List<WfStationProcessParPub> pl = wfStationProcessParPubMapper.selectByExample(exampleParent);
                if(!pl.isEmpty()){
//                    process.setParSub(pl.get(0).getParSub());
//                    process.setRuleCondition(pl.get(0).getRuleCondition());
                    tempParSub = pl.get(0).getParSub();
                    tempRuleCondition = pl.get(0).getRuleCondition();
                }
                //先找出本级之前设置的审批岗位
                WfStationProcess processChild = getWfStationProcess(org.getOrgCode());
                if(processChild != null){
                    List<WfStationProcessOrder> orderListChild = wfStationProcessOrderService.selectListByStationProcessId(processChild.getId());
                    //挑拣出本级设置的部分
                    List<WfStationProcessOrder> selfList = orderListChild.stream().filter(m -> m.getSourceFrom() != null && m.getSourceFrom().equals(StationProcessContant.OrderSourceFrom.SELF_SET.getValue())).collect(Collectors.toList());
                    //拷贝授权规则
                    selfList = selfList.stream().peek(m -> {
                        List<WfAuthRule> selfRule = getWfAuthRule(m.getId());
                        m.setId(UuidUtil.get32UUID());
                        if(!selfRule.isEmpty()){
                            m.setAuthRules(selfRule.stream().peek(x -> {
                                x.setStationProcessOrderId(m.getId());
                                x.setId(UuidUtil.get32UUID());
                            }).collect(Collectors.toList()));
                        }
                    }).collect(Collectors.toList());
                    if(StationProcessContant.ParSubType.NOPARTI.getValue().equals(tempParSub)){
                        //不参与下级 清除最后一个节点的授权规则
                        if(!selfList.isEmpty()){
                            selfList.get(selfList.size() -1).setAuthRules(null);
                            selfList.get(selfList.size() -1).setLastStation(Constant.COMMON_YES_OR_NO_YES);
                        }
                        //再往上找，如果找到参与则停止将找到的规则及上级岗位拿过来，如果找不到则一直往上找直至顶级
                        OrgHasParent pTree = sysOrgFeignService.getOrgListByOrgIdUpHasNoSelfWithTree(org.getId());
                        if(pTree != null && pTree.getParent() !=null){
                            reloadFromUpOrgStations(processChild, orderListChild, selfList, pTree.getParent());
                        }
                    }else {
                        Map<String, String> idsMap = new HashMap<>();
                        //参与下级审批
                        List<WfStationProcessOrder> listPar = orderList.stream().peek(m -> {
                            String newId = UuidUtil.get32UUID();
                            idsMap.put(m.getId(), newId);
                            //把授权规则也拷贝一下
                            List<WfAuthRule> parRule = getWfAuthRule(m.getId());
                            m.setId(newId);
                            m.setSourceFrom(StationProcessContant.OrderSourceFrom.PARENT_SET.getValue());
                            m.setStationProcessId(processChild.getId());
                            //重新设置顺序
                            m.setApproveOrder(orderListChild.size() + m.getApproveOrder());
                            //重新设置授权规则的岗位id
                            if(!parRule.isEmpty()){
                                m.setAuthRules(parRule.stream().peek(x -> {
                                    x.setStationProcessOrderId(m.getId());
                                    x.setId(UuidUtil.get32UUID());
                                }).collect(Collectors.toList()));
                            }
                        }).collect(Collectors.toList());
                        //把授权规则旧的ToStationId替换成新的ToStationId,没有新的保持不变
                        for(WfStationProcessOrder wfStationProcessOrder : listPar){
                            if(wfStationProcessOrder.getAuthRules() != null && !wfStationProcessOrder.getAuthRules().isEmpty()){
                                for(WfAuthRule authRule : wfStationProcessOrder.getAuthRules()){
                                    if(idsMap.containsKey(authRule.getToStationId())){
                                        authRule.setToStationId(idsMap.get(authRule.getToStationId()));
                                    }
                                }
                            }
                        }
                        //本级设置的加上上级重新设置的加载一起重新保存哈
                        if(!selfList.isEmpty()){
                            selfList.get(selfList.size() -1).setAuthRules(null);
                            selfList.get(selfList.size() -1).setLastStation(Constant.COMMON_YES_OR_NO_YES);
                            //设置本级最后一个节点的授权规则
                            if(StationProcessContant.ParSubType.CONDITIONAL.getValue().equals(tempParSub)){
//                                String rule = process.getRuleCondition();
                                WfAuthRule ruleNew = new WfAuthRule();
                                ruleNew.setId(UuidUtil.get32UUID());
                                ruleNew.setToStationId("end");
                                ruleNew.setRuleCondition("!(" + tempRuleCondition + ")");
                                ruleNew.setRuleType("2");
                                ruleNew.setStationProcessOrderId(selfList.get(selfList.size() -1).getId());
                                List<WfAuthRule> rl = new ArrayList<>();
                                rl.add(ruleNew);
                                selfList.get(selfList.size() -1).setAuthRules(rl);
                            }
                            //无条件参与时设置成永远为false的规则
                            if(StationProcessContant.ParSubType.UNCONDTIONNAL.getValue().equals(tempParSub)){
                                String rule = "1 < 0";
                                WfAuthRule ruleNew = new WfAuthRule();
                                ruleNew.setId(UuidUtil.get32UUID());
                                ruleNew.setToStationId("end");
                                ruleNew.setRuleCondition(rule);
                                ruleNew.setRuleType("2");
                                ruleNew.setStationProcessOrderId(selfList.get(selfList.size() -1).getId());
                                List<WfAuthRule> rl = new ArrayList<>();
                                rl.add(ruleNew);
                                selfList.get(selfList.size() -1).setAuthRules(rl);
                            }
                        }
                        selfList.addAll(listPar);
                    }
                    //删掉之前设置的审批岗位
                    deleteStationOrder(processChild);
                    //重新添加组织好的审批岗位和授权规则
                    reSetStationProcessOrder(selfList);
                    //发布流程
                    publishWfStationProcess(processChild, selfList);
                    log.info("已处理" + org.getOrgCode() + ":" + org.getOrgName() + "----------------");
                    //递归处理下级数据
                    if(!org.getChildren().isEmpty()){
                        reloadChildrenStationProcess(org.getChildren(), processChild.getOrgCode());
                    }
                }else{
                    //当前单位没发布流程
                    //参与下级审批的时候,如果本级还没有发布流程则主动生成只含上级岗位的流程，是这样的吗
                    /*if(!StationProcessContant.ParSubType.NOPARTI.getValue().equals(tempParSub)){
                        WfStationProcess processChildNew =  new WfStationProcess();
                        processChildNew.setId(UuidUtil.get32UUID());
                        processChildNew.setVersion(1);
                        processChildNew.setOrgCode(org.getOrgCode());
                        processChildNew.setOrgName(org.getOrgName());
                        processChildNew.setFolderCode(this.folderCode);
                        processChildNew.setModuleType(process.getModuleType());
                        processChildNew.setCreater(userVO.getId());
                        processChildNew.setCreateTime(new Date());
                        processChildNew.setParSub(tempParSub);
                        processChildNew.setRuleCondition(tempRuleCondition);
                        //参与下级审批
                        Map<String, String> idsMap = new HashMap<>();
                        List<WfStationProcessOrder> listPar = orderList.stream().peek(m -> {
                            String newId = UuidUtil.get32UUID();
                            idsMap.put(m.getId(), newId);
                            //把授权规则也拷贝一下
                            List<WfAuthRule> parRule = getWfAuthRule(m.getId());
                            m.setId(newId);
                            m.setSourceFrom(StationProcessContant.OrderSourceFrom.PARENT_SET.getValue());
                            m.setStationProcessId(processChildNew.getId());
                            //重新设置授权规则的岗位id
                            if(!parRule.isEmpty()){
                                m.setAuthRules(parRule.stream().peek(x -> {
                                    x.setStationProcessOrderId(m.getId());
                                    x.setId(UuidUtil.get32UUID());
                                }).collect(Collectors.toList()));
                            }
                        }).collect(Collectors.toList());
                        //把授权规则旧的ToStationId替换成新的ToStationId,没有新的保持不变
                        for(WfStationProcessOrder wfStationProcessOrder : listPar){
                            if(wfStationProcessOrder.getAuthRules() != null && !wfStationProcessOrder.getAuthRules().isEmpty()){
                                for(WfAuthRule authRule : wfStationProcessOrder.getAuthRules()){
                                    if(idsMap.containsKey(authRule.getToStationId())){
                                        authRule.setToStationId(idsMap.get(authRule.getToStationId()));
                                    }
                                }
                            }
                        }
                        //重新添加组织好的审批岗位和授权规则
                        reSetStationProcessOrder(listPar);
                        //发布流程
                        publishWfStationProcess(processChildNew, listPar);
                        log.info("已处理" + org.getOrgCode() + ":" + org.getOrgName() + "----------------");
                    }*/
                    //递归处理下级数据
                    if(!org.getChildren().isEmpty()){
                        reloadChildrenStationProcess(org.getChildren(), org.getOrgCode());
                    }
                }
            }
        }else{
            //上级没发布流程，直接处理本级数据
            for(OrgHasChildren child : children){
                //先找出本级之前设置的审批岗位
                WfStationProcess processChild = getWfStationProcess(child.getOrgCode());
                if(processChild != null){
                    List<WfStationProcessOrder> orderListChild = wfStationProcessOrderService.selectListByStationProcessId(processChild.getId());
                    //挑拣出本级设置的部分
                    List<WfStationProcessOrder> selfList = orderListChild.stream().filter(m -> m.getSourceFrom() != null && m.getSourceFrom().equals(StationProcessContant.OrderSourceFrom.SELF_SET.getValue())).collect(Collectors.toList());
                    //拷贝授权规则
                    selfList = selfList.stream().peek(m -> {
                        List<WfAuthRule> selfRule = getWfAuthRule(m.getId());
                        m.setId(UuidUtil.get32UUID());
                        if(!selfRule.isEmpty()){
                            m.setAuthRules(selfRule.stream().peek(x -> {
                                x.setStationProcessOrderId(m.getId());
                                x.setId(UuidUtil.get32UUID());
                            }).collect(Collectors.toList()));
                        }
                    }).collect(Collectors.toList());
                    //清除最后一个节点的授权规则
                    if(!selfList.isEmpty()){
                        selfList.get(selfList.size() -1).setAuthRules(null);
                        selfList.get(selfList.size() -1).setLastStation(Constant.COMMON_YES_OR_NO_YES);
                    }
                    //再往上找，如果找到参与则停止将找到的规则及上级岗位拿过来，如果找不到则一直往上找直至顶级
                    OrgHasParent pTree = sysOrgFeignService.getOrgListByOrgIdUpHasNoSelfWithTree(child.getId());
                    if(pTree != null && pTree.getParent() !=null){
                        reloadFromUpOrgStations(processChild, orderListChild, selfList, pTree.getParent());
                    }
                    //删掉之前设置的审批岗位
                    deleteStationOrder(processChild);
                    //重新添加组织好的审批岗位和授权规则
                    reSetStationProcessOrder(selfList);
                    //发布流程
                    publishWfStationProcess(processChild, selfList);
                    log.info("已处理" + child.getOrgCode() + ":" + child.getOrgName() + "----------------");
                    //递归处理下级数据
                    if(!child.getChildren().isEmpty()){
                        reloadChildrenStationProcess(child.getChildren(), processChild.getOrgCode());
                    }
                }else{
                    //本级也没发布，那么再往下搞吧
                    if(!child.getChildren().isEmpty()){
                        reloadChildrenStationProcess(child.getChildren(), child.getOrgCode());
                    }
                }
            }
        }
    }
    
    private boolean reloadFromUpOrgStations(WfStationProcess processChild, List<WfStationProcessOrder> orderListChild, List<WfStationProcessOrder> selfList, OrgHasParent org){
        try{
            WfStationProcess process = getWfStationProcess(org.getOrgCode());
            if(process !=null){
                //上级设置的岗位
                List<WfStationProcessOrder> orderList = wfStationProcessOrderService.selectListByStationProcessId(process.getId());
                String tempParSub = process.getParSub();
                String tempRuleCondition = process.getRuleCondition();
                //先看看上级有没有为自己特殊指定参与条件，有多条只取其中一条,然后用这个条件去替换主设置
                WfStationProcessParPubExample exampleParent = new WfStationProcessParPubExample();
                WfStationProcessParPubExample.Criteria criteriaParent = exampleParent.createCriteria();
                criteriaParent.andStationProcessIdEqualTo(process.getId());
                criteriaParent.andOrgIdEqualTo(org.getId());
                List<WfStationProcessParPub> pl = wfStationProcessParPubMapper.selectByExample(exampleParent);
                if(!pl.isEmpty()){
                    tempParSub = pl.get(0).getParSub();
                    tempRuleCondition = pl.get(0).getRuleCondition();
                }
                if(StationProcessContant.ParSubType.CONDITIONAL.getValue().equals(tempParSub) ||
                        StationProcessContant.ParSubType.UNCONDTIONNAL.getValue().equals(tempParSub)){
                    Map<String, String> idsMap = new HashMap<>();
                    //参与下级审批
                    List<WfStationProcessOrder> listPar = orderList.stream().peek(m -> {
                        String newId = UuidUtil.get32UUID();
                        idsMap.put(m.getId(), newId);
                        //把授权规则也拷贝一下
                        List<WfAuthRule> parRule = getWfAuthRule(m.getId());
                        m.setId(newId);
                        m.setSourceFrom(StationProcessContant.OrderSourceFrom.PARENT_SET.getValue());
                        m.setStationProcessId(processChild.getId());
                        //重新设置顺序
                        m.setApproveOrder(orderListChild.size() + m.getApproveOrder());
                        //重新设置授权规则的岗位id
                        if(!parRule.isEmpty()){
                            m.setAuthRules(parRule.stream().peek(x -> {
                                x.setStationProcessOrderId(m.getId());
                                x.setId(UuidUtil.get32UUID());
                            }).collect(Collectors.toList()));
                        }
                    }).collect(Collectors.toList());
                    //把授权规则旧的ToStationId替换成新的ToStationId,没有新的保持不变
                    for(WfStationProcessOrder wfStationProcessOrder : listPar){
                        if(wfStationProcessOrder.getAuthRules() != null && !wfStationProcessOrder.getAuthRules().isEmpty()){
                            for(WfAuthRule authRule : wfStationProcessOrder.getAuthRules()){
                                if(idsMap.containsKey(authRule.getToStationId())){
                                    authRule.setToStationId(idsMap.get(authRule.getToStationId()));
                                }
                            }
                        }
                    }
                    //本级设置的加上上级重新设置的加载一起重新保存哈
                    if(!selfList.isEmpty()){
                        selfList.get(selfList.size() -1).setAuthRules(null);
                        selfList.get(selfList.size() -1).setLastStation(Constant.COMMON_YES_OR_NO_YES);
                        //设置本级最后一个节点的授权规则
                        if(StationProcessContant.ParSubType.CONDITIONAL.getValue().equals(tempParSub)){
                            WfAuthRule ruleNew = new WfAuthRule();
                            ruleNew.setId(UuidUtil.get32UUID());
                            ruleNew.setToStationId("end");
                            ruleNew.setRuleCondition("!(" + tempRuleCondition + ")");
                            ruleNew.setRuleType("2");
                            ruleNew.setStationProcessOrderId(selfList.get(selfList.size() -1).getId());
                            List<WfAuthRule> rl = new ArrayList<>();
                            rl.add(ruleNew);
                            selfList.get(selfList.size() -1).setAuthRules(rl);
                        }
                        //无条件参与时设置成永远为false的规则
                        if(StationProcessContant.ParSubType.UNCONDTIONNAL.getValue().equals(tempParSub)){
                            String rule = "1 < 0";
                            WfAuthRule ruleNew = new WfAuthRule();
                            ruleNew.setId(UuidUtil.get32UUID());
                            ruleNew.setToStationId("end");
                            ruleNew.setRuleCondition(rule);
                            ruleNew.setRuleType("2");
                            ruleNew.setStationProcessOrderId(selfList.get(selfList.size() -1).getId());
                            List<WfAuthRule> rl = new ArrayList<>();
                            rl.add(ruleNew);
                            selfList.get(selfList.size() -1).setAuthRules(rl);
                        }
                    }
                    selfList.addAll(listPar);
                    return true;
                }
            }
            if(org.getParent() != null){
                return reloadFromUpOrgStations(processChild, orderListChild, selfList, org.getParent());
            }else{
                return false;
            }
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }
    
    protected void publishWfStationProcess(WfStationProcess processChild, List<WfStationProcessOrder> selfList) throws Exception{
        Model templateModel = modelService.findByProcessKey("process_template_" + selfList.size());//获取流程模型扩展
        if (ObjectUtils.isEmpty(templateModel)) {
            throw new BusinessException("流程模块[process_template_" + selfList.size() + "]不存在!");
        }
        String templateModelId = templateModel.getId(), processName, processKey, processCode;
        Model model = modelService.findByOrgCodeAndProcessCode(processChild.getOrgCode(), processChild.getFolderCode());
        if (ObjectUtils.isEmpty(model)) {//第一次发布,新增一个模型扩展
            processName = this.folderName;
            processKey = "wf_" + processChild.getOrgCode().replaceAll("#", ".") + "_" + processChild.getFolderCode();
            processCode = processChild.getFolderCode();
            model = modelService.newModel(processChild.getOrgCode(), processName, processKey, processCode, userVO);
        }
        model.setName(this.folderName);
        Model newModel = modelService.copyModel(templateModelId, model.getId(), model.getOrgCode(), model.getName(), model.getKey(), model.getProcessCode(), selfList);//复制模型
        
        modelService.deployModelExtendAndSetAuthor(newModel, processChild, userVO, selfList);//根据模板发布流程
    }
    
    protected WfStationProcess getWfStationProcess(String orgCode){
        WfStationProcessExample example = new WfStationProcessExample();
        WfStationProcessExample.Criteria criteria = example.createCriteria();
        criteria.andOrgCodeEqualTo(orgCode);
        criteria.andFolderCodeEqualTo(this.folderCode);
        List<WfStationProcess> processList = wfStationProcessMapper.selectByExample(example);
        if(!processList.isEmpty()){
            return processList.get(0);
        }else{
            return null;
        }
    }
    
    protected List<WfAuthRule> getWfAuthRule(String processOrderId){
        WfAuthRuleExample example = new WfAuthRuleExample();
        WfAuthRuleExample.Criteria criteria = example.createCriteria();
        criteria.andStationProcessOrderIdEqualTo(processOrderId);
        return wfAuthRuleMapper.selectByExample(example);
    }
    
    protected void deleteStationOrder(WfStationProcess processChild){
        wfStationProcessOrderService.deleteByStationProcessId(processChild.getId());
    }
    
    protected void reSetStationProcessOrder(List<WfStationProcessOrder> selfList){
        for(WfStationProcessOrder wfStationProcessOrder : selfList){
            if(wfStationProcessOrder.getLastStation() == null){
                wfStationProcessOrder.setLastStation(Constant.COMMON_YES_OR_NO_NO);
            }
            if(wfStationProcessOrder.getNeedKey() == null){
                wfStationProcessOrder.setNeedKey(Constant.COMMON_YES_OR_NO_NO);
            }
            wfStationProcessOrderMapper.insertSelective(wfStationProcessOrder);
            if(wfStationProcessOrder.getAuthRules() != null && !wfStationProcessOrder.getAuthRules().isEmpty()){
                for(WfAuthRule authRule : wfStationProcessOrder.getAuthRules()){
                    wfAuthRuleMapper.insertSelective(authRule);
                }
            }
        }
    }
}
